package com.signway.module_signway_adapter_common_3;

import android.content.Context;
import android.util.Log;

import com.signway.open.api.IOpenDeviceApi;

import java.util.regex.Pattern;

//import android.os.signwaymanager.SignwayManager;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2011-2019, by your Signway, All rights reserved.
 * -----------------------------------------------------------------
 *
 * ProjectName: droid5119
 *
 * Author: Administrator
 *
 * Email: pf.wang@Signway.cn
 *
 * Description:
 *
 * -----------------------------------------------------------------
 * 2019-5-29 : Create SignwayDeviceApi.java
 * -----------------------------------------------------------------
 *
 *
 * 待测。放到moudle里调用不了app的方法，放到app里，moudle里用不了。。。。。。。。
 *
 */
public class SignwayDeviceApi implements IOpenDeviceApi {

    private static final String TAG = "SignwayDeviceApi";
    private static SignwayDeviceApi mSignwayDeviceApi;
    private static Context mContext;
//    private SignwayManager signwayManager;

    private static final int SFCT_CMD_EEPROM_READ = 0x8102B0;
    private static final int SFCT_CMD_EEPROM_WRITE = 0x8102B1;

    private static final int AUTH_TYPE_ACTIVE = 1;         //激活类型3bytes
    private static final int AUTH_TYPE_DATE = 2;             //累计播放时长8
    private static final int AUTH_TYPE_TOTAL_TIME = 3;         //授权总时长8
    private static final int AUTH_TYPE_END_DATE = 4;           //授权结束日期10
    private static final int AUTH_TYPE_START_DATE = 5;         //授权开始日期10
    private final int dm2016AuthPos = 10;          //激活类型占了3位，起始位置：第10位
    private final int dm2016AuthDatePos = 13;      //累计播放时长占了8位，起始位置：第13位
    private final int dm2016AuthEndTimePos = 21;   //授权总时长占了8位，起始位置：第21位
    private final int dm2016AuthEndDatePos = 29;   //授权结束日期占了10位，起始位置：第29位
    private final int dm2016AuthStartDatePos = 39;   //授权开始日期占了10位，起始位置：第39位


    public static synchronized SignwayDeviceApi getInstance(Context context) {
        mContext = context;
        if (mSignwayDeviceApi == null) {
            mSignwayDeviceApi = new SignwayDeviceApi(context);
        }
        return mSignwayDeviceApi;
    }

    public SignwayDeviceApi(Context context) {
        this.mContext = context;
        Log.i(TAG, "SignwayDeviceApi: ");
//        signwayManager = SignwayManager.getInstance(mContext);
    }


    @Override
    public void OpenJoint() {
        Log.i(TAG, "OpenJoint: ");
//        if (PinJieRTDComm.getRTDCommInstance().RTDComm_ttys4_Thread.isAlive() && PinJieRTDComm.getRTDCommInstance().RTDComm_ttys1_Thread.isAlive()) {
//            LogUtil.d("pinJieRTDComm readRTDThread is isAlive");
//            return;
//        }
//        PinJieRTDComm.getRTDCommInstance().start();
    }

    @Override
    public int openUart(String device, int baud) {
        Log.i(TAG, "openUart: ");
//        return signwayManager.serialDeviceOpen(device, baud);
        return -1;
    }

    @Override
    public int closeUart(int fd) {
        Log.i(TAG, "closeUart: ");
//        return signwayManager.serialDeviceClose(fd);
        return -1;
    }

    @Override
    public int readUart(int fd, byte[] buf, int len) {
        Log.i(TAG, "readUart: ");
//        return signwayManager.serialRead(fd, buf, len);
        return 0;
    }

    @Override
    public int writeUart(int fd, byte[] buf, int len) {
        Log.i(TAG, "writeUart: ");
//        return signwayManager.serialWrite(fd, buf, len);
        return 0;
    }

    @Override
    public int ReadEepromInfo(int pos, byte[] buff, int len) {
        Log.i(TAG, "ReadEepromInfo: ");
//        int fd = signwayManager.i2cDeviceOpen("/dev/sfct");
//        int length = signwayManager.i2cReadWrite(fd, SFCT_CMD_EEPROM_READ, pos, buff, len);
//        signwayManager.i2cDeviceClose(fd);
//        return length;
        return 0;
    }

    @Override
    public int WriteEepromInfo(int pos, byte[] buff, int len) {
        Log.i(TAG, "WriteEepromInfo: ");
//        int fd = signwayManager.i2cDeviceOpen("/dev/sfct");
//        int length = signwayManager.i2cReadWrite(fd, SFCT_CMD_EEPROM_WRITE, pos, buff, len);
//        signwayManager.i2cDeviceClose(fd);
//        return length;
        return 0;
    }

    @Override
    public void setAuthType(int value) {
        Log.i(TAG, "setAuthType: ");
        String strAuth = value + "";
        int strLen = strAuth.length();
        if (strLen != 3) {
            for (int i = 0; i < 3 - strLen; i++) {
                strAuth = "0" + strAuth;
            }
        }
        byte[] buffer = strAuth.getBytes();
        for (int i = 0; i < buffer.length; i++) {
            Log.i(TAG, "bufff--write-" + buffer[i]);
        }
//        int aaa = signwayManager.setApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 3);

    }

    @Override
    public int getAuthType() {
        Log.i(TAG, "getAuthType: ");
        byte[] buffer = new byte[3];
        int len = 3;
        int auth = 0;
//        int length = signwayManager.getApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 3);
        int length = -1;
        if (length != len) {
            Log.e(TAG, "read getAuth failed");
        }
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buffer[i] == -1) {
                    break;
                }
            }
            auth = Integer.parseInt(new String(buffer, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get auth failed: " + e.toString());
            auth = 0;
        }

        if ((auth != 0x00) && (auth != 0xA0) && (auth != 0xAB)) {
            auth = 0x00;
        }
        return auth;
    }

    @Override
    public void setWorkTime(long value) {
        Log.i(TAG, "setWorkTime: ");
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
        for (int i = 0; i < buffer.length; i++) {
            Log.i(TAG, "set work time buffer=" + buffer[i]);
        }
//        signwayManager.setApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 8);
        Log.i(TAG, "set work time =" + value);
    }

    @Override
    public long getWorkTime() {
        Log.i(TAG, "getWorkTime: ");
        byte[] buffer = new byte[8];
        int len = 8;
//        int length = signwayManager.getApkAuthInformations(AUTH_TYPE_ACTIVE, buffer, 8);
        int length = -1;
        if (length != len) {
            Log.i(TAG, "read getWorkTime failed");
        }
        for (int i = 0; i < buffer.length; i++) {
            Log.i(TAG, "get work time buffer 1 =" + buffer[i]);
        }
        long date = 0;
        int date1 = 0;
        try {
            int off = 0;
            for (int i = 0; i < len; i++) {
                off = i;
                if (buffer[i] == -1) {
                    break;
                }
            }

            for (int i = 0; i < buffer.length; i++) {
                Log.i(TAG, "get work time buffer 2 =" + buffer[i] + "--------" + off);
            }
            date = Long.parseLong(new String(buffer, 0, off + 1));
            date1 = Integer.parseInt(new String(buffer, 0, off + 1));
        } catch (Exception e) {
            Log.i(TAG, "get work time failed: " + e.toString());
            date = 0;
        }
        Log.i(TAG, "get work time =" + date + "--" + date1);
        return date;
    }

    @Override
    public void setAuthTotalTime(long value) {
        Log.i(TAG, "setAuthTotalTime: ");
        String strTime = value + "";
        int len = strTime.length();
        if (len != 8) {
            for (int i = 0; i < 8 - len; i++) {
                strTime = "0" + strTime;
            }
        }
        byte[] buffer = strTime.getBytes();
//        signwayManager.setApkAuthInformations(AUTH_TYPE_TOTAL_TIME, buffer, 8);

    }

    @Override
    public long getAuthTotalTime() {
        Log.i(TAG, "getAuthTotalTime: ");
        byte[] buffer = new byte[8];
//        return signwayManager.getApkAuthInformations(AUTH_TYPE_TOTAL_TIME, buffer, 8);
        return 0;
    }

    @Override
    public void setAuthEndDate(String date) {
        Log.i(TAG, "setAuthEndDate: ");
        byte[] buffer = date.getBytes();
//        signwayManager.setApkAuthInformations(AUTH_TYPE_END_DATE, buffer, 10);
    }

    @Override
    public String getAuthEndDate() {
        Log.i(TAG, "getAuthEndDate: ");
        byte[] buffer = new byte[10];
//        signwayManager.getApkAuthInformations(AUTH_TYPE_END_DATE, buffer, 10);
        return "";
    }

    @Override
    public void setAuthStartDate(String date) {
        Log.i(TAG, "setAuthStartDate: ");
        byte[] buffer = date.getBytes();
//        signwayManager.setApkAuthInformations(AUTH_TYPE_START_DATE, buffer, 10);

    }

    @Override
    public String getAuthStartDate() {
        Log.i(TAG, "getAuthStartDate: ");
        byte[] buffer = new byte[10];
//        signwayManager.getApkAuthInformations(AUTH_TYPE_START_DATE, buffer, 10);
        return "";
    }

    @Override
    public void setSN(String sn) {
        Log.i(TAG, "setsn: " + sn);
//        signwayManager.setSN(sn);
    }

    @Override
    public String getSN() {
//        String sn = signwayManager.getSN();
        String sn = "";
        Pattern pattern = Pattern.compile("[0-9a-zA-Z]");
        if (!pattern.matcher(sn).matches()) {
            sn = "";
        }
        Log.i(TAG, "getsn: " + sn);
        return sn;
    }

    @Override
    public void setMAC(String mac) {
        Log.i(TAG, "Setmac: " + mac);
//        signwayManager.setMAC(mac);

    }

    @Override
    public String getMAC() {
        String mac = "null";
//        String mac = signwayManager.getMAC();
        Log.i(TAG, "Gmac: " + mac);
        return mac;
    }

    @Override
    public boolean setScreenRotation(int id, int rotation) {
        return false;
    }

    @Override
    public int getScreenRotation(int id) {
        return 0;
    }
}
